<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2016/2/27
 * Time: 16:56
 */
class AdminUsersModel extends BaseModel{

    protected $tableName = "admin_users";//数据库表名

    protected $_validate = array(
        array('user_id','require','登录名必须!'),
        array('user_id','','帐号名称已经存在！',0,'unique',3),
        array('password','require','密码不能为空'),
    );

    /**
     * @param $user_id
     * @param $pass
     * @return bool
     * 登录验证
     */
    public function checkLogin($user_name,$pass){
        $row = M('AdminUsers')
            ->field('user_id,nickname,password,purview,purview_id')
            ->where("user_id='{$user_name}'")
            ->select();

        if(!empty($row) && pwCheck($pass,$row[0]['password'])){
            $user['user_id'] = $row[0]['user_id'];
            $user['purview'] = $row[0]['purview'];
            $user['nickname']= $row[0]['nickname'];
            $map['id']    = array('in',$row[0]['purview_id']);
            $res = M('Purview')->field('purview_url')->where($map)->select();
            if(!empty($res)){
                foreach($res as $v){
                    $purview_url[] = $v['purview_url'];
                }
            }else{
                $purview_url = array();
            }
            $user['purview_url'] = $purview_url;

            return $user;
        }
        return false;
    }

    /**
     * @param $where
     * @return array
     * 获取管理员列表
     */
    public function getAdminList($where){

        $res = $this->where($where)->select();

        return !empty($res) ? $res : array();
    }

    /**
     * @param $data
     * 保存数据
     */
    public function saveAdmins($data){

        $insert =  array(
            'id'        => !empty($data['id'])   ? $data['id'] : null,
            'user_id'   => !empty($data['user-id'])   ? $data['user-id'] : '',
            'nickname'  => !empty($data['nick-name']) ? $data['nick-name'] : '',
            'password'  => !empty($data['password'])  ? md5($data['password']) : '',
            'purview'   => !empty($data['purview'])   ? $data['purview'] : '',
            'purview_id'=> !empty($data['purview-id'])? $data['purview-id'] : '',
            'add_time'  => !empty($data['add-time'])  ? $data['add-time'] : time(),
            'del'       => 1,
        );

        if ($this->create($insert)){

            $res = (null ==$insert['id']) ?  $this->add($insert) : $this->save($insert);

            if($res){
                $return['status'] = true;
                $return['info']   = "操作成功";
            }else{
                $return['status'] = false;
                $return['info']   = $this->getDbError();
            }
        }else{
            $return['status'] = false;
            $return['info']   = $this->getError();
        }

        return $return;
    }

    /**
     * @param $id
     * @return mixed
     * 通过id获取管理员信息
     */
    public function getInfoById($id){

        $res   = $this->where('id='.$id)->select();
        return !empty($res) ? $res[0]:array();
    }
}